package com.ghsc.ghxc.jsb.ui.my.activity

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.getCompatColor
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.widget.CustomRefreshListener
import com.cn.appcore.widget.RefreshHelper
import com.flyco.roundview.RoundLinearLayout
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.BarRefreshListBinding
import com.ghsc.ghxc.jsb.ui.message.view.RouterUtils
import com.ghsc.ghxc.jsb.ui.my.bean.FollowFansBean
import com.ghsc.ghxc.jsb.view.NotDataView
import com.scwang.smart.refresh.layout.api.RefreshLayout

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.my.activity
 * 类名：FollowFansListActivity
 * 创建时间：2024/5/17 15:27
 * 创建人 @author： xiaolin
 * 描述 ：Todo.... 关注 粉丝
 */
class FollowFansListActivity  : BaseVMActivity<FollowFansListViewModel, BarRefreshListBinding>() {
    override fun attachLayoutRes(): Int {
        return R.layout.bar_refresh_list
    }


    override fun attachVMClass(): Class<FollowFansListViewModel>? {
        return FollowFansListViewModel::class.java
    }

    private val mAdapter: FollowFansListAdapter by lazy { FollowFansListAdapter(sign) }
    var mList = arrayListOf<FollowFansBean>()
    private var notDataView: View? = null

    var itemPosition = -1 //记录点击的某一个的位置


    var bundle: Bundle? = null
    var sign: Int = 1
    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }

    override fun initView() {
        bundle = intent.extras
        sign = bundle!!.getInt("sign", 1)
        binding.run {
            bar.topTitle.text = if (sign == 1) "我的关注" else "我的粉丝"
            bar.topLeftView.setOnClickListener { finish() }
            bar.topRightView.visibility = View.GONE
        }

        binding.rvList.layoutManager = LinearLayoutManager(this)
        binding.rvList.adapter = mAdapter
        mAdapter.setNewInstance(mList)
        notDataView =
            NotDataView.notDataView(this, binding.rvList, binding.refreshLayout, "暂无数据")
        mAdapter.setOnItemClickListener { adapter, view, position ->
        }
        mAdapter.addChildClickViewIds(R.id.llBtnFollow)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (R.id.llBtnFollow == view.id) {
                itemPosition = position
                //关注   取消关注
                val map: java.util.HashMap<String, Any> = java.util.HashMap()
                map["tuid"] = mAdapter.getItem(position).uid
                GsonUtil.mapToJsonStrObjEncryption(map)
                    ?.let { it1 -> mViewModel.onUserFollowMethod(it1) }
            }
        }
        initRefreshLayout()
        pageNo = 1
        getListData()
    }

    private fun initRefreshLayout() {
        RefreshHelper.initRefreshLayout(binding.refreshLayout, object : CustomRefreshListener {
            override fun _onRefresh(refreshLayout: RefreshLayout?) {
                pageNo = 1
                getListData()
            }

            override fun _onLoadMore(refreshLayout: RefreshLayout?) {
                pageNo++
                getListData()
            }
        })
    }


    private fun getListData() {
        if (sign == 1) {
            val map: HashMap<String, Any> = HashMap()
            map["pindex"] = pageNo
            map["psize"] = pageSize
            GsonUtil.mapToJsonStrObjEncryption(map)
                ?.let { it1 -> mViewModel.onMyFollowListMethod(it1) }
        } else {
            val map: HashMap<String, Any> = HashMap()
            map["pindex"] = pageNo
            map["psize"] = pageSize
            GsonUtil.mapToJsonStrObjEncryption(map)
                ?.let { it1 -> mViewModel.onMyFansListMethod(it1) }
        }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@FollowFansListActivity) {
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                showToast(it)
            }
            againLoginMsg.observe(this@FollowFansListActivity) {
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }
            onUserFollowData.observe(this@FollowFansListActivity) {
                //关注  取消关注

                if (sign == 1) {
                    //我的关注。肯定是取消关注
                    if (it.data.data.flag == 1) {
                        //关注成功
                    } else {
                        mAdapter.data.removeAt(itemPosition)
                        if (mAdapter.data.size > 0)
                            mAdapter.notifyDataSetChanged()
                        else {
                            var mList = arrayListOf<FollowFansBean>()
                            mAdapter.setEmptyView(notDataView!!)
                            mAdapter.replaceData(mList)
                        }
                    }
                } else {
                    if (it.data.data.flag == 1) {
                        //关注成功
                        mAdapter.data[itemPosition].is_follow = 1
                        mAdapter.notifyItemChanged(itemPosition)
                    } else {
                        //取消关注成功
                        mAdapter.data[itemPosition].is_follow = 0
                        mAdapter.notifyItemChanged(itemPosition)

                    }
                }
//                isFollow= it.data.data.flag==1
            }
            onMyFollowListData.observe(this@FollowFansListActivity) {
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                if (pageNo == 1) {
                    if (it.data.list.isNullOrEmpty()) {
                        mAdapter.setEmptyView(notDataView!!)
                        mAdapter.replaceData(it.data.list)
                    } else {
                        mAdapter.replaceData(it.data.list)
                    }
                } else {
                    if (!it.data.list.isNullOrEmpty())
                        mAdapter.addData(it.data.list)
                }
            }
            onMyFansListData.observe(this@FollowFansListActivity) {
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                if (pageNo == 1) {
                    if (it.data.list.isNullOrEmpty()) {
                        mAdapter.setEmptyView(notDataView!!)
                        mAdapter.replaceData(it.data.list)
                    } else {
                        mAdapter.replaceData(it.data.list)
                    }
                } else {
                    if (!it.data.list.isNullOrEmpty())
                        mAdapter.addData(it.data.list)
                }
            }


        }
    }


    inner class FollowFansListAdapter (sign:Int)     : BaseQuickAdapter<FollowFansBean, BaseViewHolder>(
        R.layout.list_follow_item) {


        var sign:Int=1
        init {
            this.sign=sign
        }

        override fun convert(helper: BaseViewHolder, item: FollowFansBean) {

            GlideUtil.ImageLoad(this.context,item.headimg,helper.getView(R.id.userImgUrl))
            helper.setText(R.id.nickname,item.nickname)
//
////
//            var icon = helper.getView<ImageView>(R.id.imgFollow)
            var tvFollow= helper.getView<TextView>(R.id.tvFollow)
//
            var llBtnFollow= helper.getView<RoundLinearLayout>(R.id.llBtnFollow)
            // 我的关注，1是互相关注。 0 是我关注他    我的粉丝。 1是互相关注 0是他关注我
            if (sign==1){
                //我的关注
//                icon.setImageResource(R.mipmap.icon_dynamic_focus)
                tvFollow.text="取消关注"
                tvFollow.setTextColor(this.context.getCompatColor(R.color.font_black))
                llBtnFollow.delegate.strokeColor=this.context.getCompatColor(R.color.color_xian)
                    helper.setGone(R.id.llBtnIm,true)


            }else {
                //我的粉丝
                if (item.is_follow==1){
//                    icon.setImageResource(R.mipmap.icon_dynamic_focus)
                    tvFollow.text="取消关注"
                    tvFollow.setTextColor(this.context.getCompatColor(R.color.font_black))
//                    llBtnFollow.delegate.strokeColor=this.context.getCompatColor(R.color.color_xian)
                }else{
//                    icon.setImageResource(R.mipmap.icon_dynamic_not_focus)
                    tvFollow.text="关 注"
                    tvFollow.setTextColor(this.context.getCompatColor(R.color.main_color))
//                    llBtnFollow.delegate.strokeColor=this.context.getCompatColor(R.color.main_color)
                }

                helper.setGone(R.id.llBtnIm,false)
                    .getView<RoundLinearLayout>(R.id.llBtnIm).setSingleClickListener {
                        RouterUtils.startToJumpSingleChat(this.context,item.uid)
                    }
            }

        }

    }

}
